<?php

class Room extends CI_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$level_id = $this->session->userdata('logged_in');
		if ($level_id['level_id'] == null) {
			redirect('user/login', 'refresh');
		}
		if ($level_id['level_id'] == 0) {
			redirect('user', 'refresh');
		}
	}
	
	function index()
	{
		$crud = new grocery_CRUD();
		
		
		$crud->set_table('room');
		$crud->set_relation('room_type_id', 'room_type', 'room_type_name',array('deleted' => 'No'));
		$crud->required_fields('room_number', 'room_type_id','room_status');
		$crud->where('room.deleted', 'No');
		
		//$crud->fields('room_type_id', 'room_number', 'room_desc', 'room_status');
		$crud->display_as('room_number', 'Room Number');
		$crud->display_as('room_type_id', 'Room Type');
		$crud->display_as('room_desc', 'Description');
		$crud->columns('room_number', 'room_type_id', 'room_desc');
		//$crud->display_as('room_status', 'Room Status');
		//$crud->display_as('status', 'Change Status');
		$crud->set_subject('Room');
		
		
		
		$crud->callback_add_field('room_number', array(
			$this,
			'field_code_room'
		));
		$crud->callback_after_update(array(
			$this,
			'get_id_room'
		));
		$crud->callback_after_insert(array(
			$this,
			'get_id_room'
		));
		$crud->callback_delete(array(
			$this,
			'deleted_index'
		));
		
		
		$crud->add_action('', '', '', 'icon-th-list', array(
			$this,
			'viewroom_material'
		));
		
		$crud->unset_add_fields('last_modifier', 'last_modified', 'deleted');
		$crud->unset_edit_fields('room_status','last_modifier', 'last_modified', 'deleted');
		
		//$crud->callback_column('status', array($this, 'editRoom'));
		
		$level_id = $this->session->userdata('logged_in');
		if ($level_id['level_id'] == 3) {
			$crud->unset_delete();
			$crud->unset_edit();
			
			$output          = (array) $crud->render();
			$output['title'] = 'Room List';
			
			$this->load->template('room/index', $output);
		} else {
			$output          = (array) $crud->render();
			$output['title'] = 'Room List';
			
			$this->load->template('room/index', $output);
			
		}
		
		
	}
	
	
	function viewroom_material($primary_key, $row)
	{
		return site_url('room/room_material') . '/' . $row->room_id;
	}
	
		
	function field_code_room()
	{
		$query  = "select max(room_number) as max from room";
		$result = $this->db->query($query)->row();
		$string = (int) $result->max + 1;
		return '<input type="text" maxlength="4" value=" ' . $string . ' " name="room_number" style="width:auto">';
		
	}
	
	function get_id_room($post_array, $primary_key)
	{
		$sess = $this->session->userdata('logged_in');
		$this->db->update('room', array(
			'last_modifier' => $sess['user_id']
		), array(
			'room_id' => $primary_key
		));
		
	}
	function deleted_index($primary_key)
	{
		$level_id = $this->session->userdata('logged_in');
		$this->db->update('room', array(
			'deleted' => 'Yes',
			'last_modifier' => $level_id['user_id']
		), array(
			'room_id' => $primary_key
		));
	}
	
	function housekeeping()
	{
		$crud = new grocery_CRUD();
		
		$crud->set_table('room');
		$crud->set_relation('room_type_id', 'room_type', 'room_type_name',array('deleted' => 'No'));
		$crud->required_fields('room_number', 'room_type_id', 'room_status');
		$crud->where('room.deleted', 'No');
		
		$crud->display_as('room_number', 'Room Number');
		$crud->display_as('room_type_id', 'Room Type');
		$crud->display_as('room_status', 'Room Status');
		$crud->display_as('status', 'Action');
		$crud->set_subject('Housekeeping');
		
		$crud->unset_delete();
		$crud->unset_edit();
		$crud->unset_add();
		$crud->unset_read();
			
		$crud->columns('room_number', 'room_type_id', 'room_status', 'status');
				
		$crud->callback_column('room_status', array($this, 'room_status'));
		$crud->callback_column('status', array($this, 'editRoom'));
		
		
		$level_id = $this->session->userdata('logged_in');
		
			
		$output          = (array) $crud->render();
		$output['title'] = 'Room List';
			
		$this->load->template('room/housekeeping', $output);
		
	}
	
	
	function editRoom($value, $row) {
				
		if($row->room_status == '<span class="label label-warning">Dirty</span>'){
			return '<a style="color:black" class="btn btn-mini" href="' . base_url() . 'room/clean/' . $row->room_id . '">Change status to <b>Cleaning</b></a>';
		} else {
			if($row->room_status == '<span class="label label-info">Cleaning</span>'){
				return '<a style="color:black" class="btn btn-mini" href="' . base_url() . 'room/ready/' . $row->room_id . '">Change status to <b>Ready</b></a>';
			} else {
				return '<a style="color:black" class="btn btn-mini" href="' . base_url() . 'room/dirty/' . $row->room_id . '">Change status to <b>Dirty</b></a>';
				}
		
			}
    }
	
	function room_status($value, $row) {
		if($value == 'Ready') {
		return '<span class="label label-success">Ready</span>';
		}
		else if($value == 'Cleaning'){
			return '<span class="label label-info">Cleaning</span>';
		}
		else {
			return '<span class="label label-warning">Dirty</span>';
		}
	}
	
	function clean($id){
		$this->load->model('room_model');
		$this->room_model->cleaning($id);
		redirect('room/housekeeping');
	}
	
	function dirty($id){
		$this->load->model('room_model');
		$this->room_model->dirty($id);
		redirect('room/housekeeping');
	}
	
	function ready($id){
		$this->load->model('room_model');
		$this->room_model->ready($id);
		redirect('room/housekeeping');
	}
	
	
	
	
	function room_material($id)
	{
		$crud = new grocery_CRUD();
		$crud->set_table('room_material');
		$crud->set_relation('room_id', 'room', 'room_number');
		$crud->set_relation('material_id', 'material', 'material_name');
		
		$crud->set_subject('Room Material');
		
		$crud->required_fields('room_id', 'material_id', 'room_mat_qty');
		$crud->where('room_material.room_id', $id);
		$crud->where('room_material.deleted', 'No');
		$crud->columns('room_id', 'material_id', 'room_mat_qty');
		
		$crud->fields('material_id', 'room_mat_qty');
		
		$crud->callback_after_update(array(
			$this,
			'get_id_room_material'
		));
		//$crud->callback_after_insert(array($this, 'get_id_room_material'));
		$crud->callback_delete(array(
			$this,
			'deleted_room_material'
		));
		$crud->callback_field('room_mat_qty', array(
			$this,
			'id_room_material'
		));
		$crud->callback_insert(array(
			$this,
			'input_room_material'
		));
		
		
		$level_id = $this->session->userdata('logged_in');
		if ($level_id['level_id'] == 3) {
			$crud->unset_delete();
			$crud->unset_edit();
			
			$query  = "select room_number as max from room where room_id =" . $id;
			$result = $this->db->query($query)->row();
			$string = (int) $result->max;
			
			$output          = (array) $crud->render();
			$output['title'] = 'Room Material <span id="room-number">' . $string . '</span>';
			
			$this->load->template('room/room_material', $output);
		} else {
			$query  = "select room_number as max from room where room_id =" . $id;
			$result = $this->db->query($query)->row();
			$string = (int) $result->max;
			
			$output          = (array) $crud->render();
			$output['title'] = 'Room Material <span id="room-number">' . $string . '</span>';
			
			$this->load->template('room/room_material', $output);
			
		}
		
	}
	
	function max_material_qty() {
		$this->load->model('room_model');
		$material_id = $this->input->post('material_id');
		$material = $this->room_model->get_material($material_id)->row_array();
		
		echo json_encode($material['material_qty']);
	}
	
	function input_room_material($post_array)
	{
		$sess                        = $this->session->userdata('logged_in');
		$post_array['last_modifier'] = $sess['user_id'];
		
		$this->db->select('*')->from('room')->where('room_number', $post_array['room_number']);
		$room = $this->db->get()->row_array();
		unset($post_array['room_number']);
		$post_array['room_id'] = $room['room_id'];
		var_dump($post_array);
		//$query = "insert into room_material (room_id,room_material,room_mat_qty) values ()";
		
		return $this->db->insert('room_material', $post_array);
		
	}
	
	function id_room_material($value)
	{
		
		
		return '<input class="qty" type="text" maxlength="4" value="'.$value.'" name="room_mat_qty" style="width:auto"><input type="hidden" id="room_number" maxlength="4" value="" name="room_number" style="width:auto">';
		
	}
	function get_id_room_material($post_array, $primary_key)
	{
		$sess = $this->session->userdata('logged_in');
		$this->db->update('room_material', array(
			'last_modifier' => $sess['user_id']
		), array(
			'room_mat_id' => $primary_key
		));
		
	}
	function deleted_room_material($primary_key)
	{
		$level_id = $this->session->userdata('logged_in');
		$this->db->update('room_material', array(
			'deleted' => 'Yes',
			'last_modifier' => $level_id['user_id']
		), array(
			'room_mat_id' => $primary_key
		));
	}
	
	function type()
	{
		$crud = new grocery_CRUD();
		
		$crud->set_table('room_type');
		// $crud->set_relation('bed_type_id', 'bed_type', 'bed_type_name');
		$crud->where('deleted', 'No');
		
		$crud->fields('room_type_id', 'room_type_name', 'room_type_desc', 'room_type_max_adult', 'room_type_max_child', 'room_type_max_infant', 'room_type_price', 'room_type_smoking_allowed');
		
		$crud->required_fields('room_type_id', 'room_type_name', 'room_type_desc', 'room_smoking_allowed', 'room_type_price');
		
		$crud->columns('room_type_id', 'room_type_name', 'room_type_desc', 'room_type_max_adult', 'room_type_max_child', 'room_type_max_infant', 'room_type_price', 'room_type_smoking_allowed');
		
		$crud->set_subject('Room Type');
		
		$crud->display_as('room_type_id', 'Room Type ID');
		$crud->display_as('room_type_name', 'Room Type Name');
		$crud->display_as('room_type_desc', 'Room Type Description');
		$crud->display_as('room_type_max_adult', 'Total Max. Adult');
		$crud->display_as('room_type_max_child', 'Total Max. Child');
		$crud->display_as('room_type_max_infant', 'Total Max. Infant');
		$crud->display_as('room_type_smoking_allowed', 'Smoking Allowed?');
		$crud->display_as('room_type_price', 'Room Rates');
		
		$crud->callback_column('room_type_price', array(
			$this,
			'formatRupiahRoomType'
		));
		$crud->callback_field('room_type_price', array(
			$this,
			'formatPriceFieldRoomType'
		));
		
		$crud->callback_add_field('room_type_id', array(
			$this,
			'field_code_roomtype'
		));
		$crud->callback_delete(array(
			$this,
			'deleted_roomtype'
		));
		$crud->callback_after_update(array(
			$this,
			'get_id_roomtype'
		));
		$crud->callback_after_insert(array(
			$this,
			'get_id_roomtype'
		));
		$crud->callback_field('room_type_price', array($this, 'formatPriceField'));
		$crud->callback_field('room_type_max_adult', array($this, 'formatAdultField'));
		$crud->callback_field('room_type_max_child', array($this, 'formatChildField'));
		$crud->callback_field('room_type_max_infant', array($this, 'formatInfantField'));
		$crud->callback_before_insert(array($this, 'cb_before_room_type'));
		$crud->callback_before_update(array($this, 'cb_before_room_type'));
		
		$level_id = $this->session->userdata('logged_in');
		if ($level_id['level_id'] == 3) {
			$crud->unset_delete();
			$crud->unset_edit();
			
			$output          = (array) $crud->render();
			$output['title'] = 'Room Type List';
			
			$this->load->template('room/roomtype', $output);
			;
		} else {
			$output          = (array) $crud->render();
			$output['title'] = 'Room Type List';
			
			$this->load->template('room/roomtype', $output);
			;
			
		}
	}
	
	function deleted_roomtype($primary_key)
	{
		$level_id = $this->session->userdata('logged_in');
		$this->db->update('room_type', array(
			'deleted' => 'Yes',
			'last_modifier' => $level_id['user_id']
		), array(
			'room_type_id' => $primary_key
		));
		$this->db->update('room', array(
			'deleted' => 'Yes',
			'last_modifier' => $level_id['user_id']
		), array(
			'room_type_id' => $primary_key
		));
		
	}
	function field_code_roomtype()
	{
		$query  = "select max(room_type_id) as max from room_type";
		$result = $this->db->query($query)->row();
		$string = (int) $result->max + 1;
		return '<input type="text" maxlength="4" value="' . $string . '" name="room_type_id" style="width:auto">';
		
	}
	function get_id_roomtype($post_array, $primary_key)
	{
		$sess = $this->session->userdata('logged_in');
		$this->db->update('room_type', array(
			'last_modifier' => $sess['user_id']
		), array(
			'room_type_id' => $primary_key
		));
		
	}
	
	
	function bed()
	{
		$crud = new grocery_CRUD();
		
		$crud->set_table('bed_type');
		$crud->set_subject('Bed Type');
		$crud->required_fields('bed_type_id', 'bed_type_name');
		$crud->columns('bed_type_id', 'bed_type_name', 'bed_type_desc');
		$crud->display_as('bed_type_id', 'Bed Code');
		$crud->display_as('bed_type_name', 'Bed Name');
		$crud->display_as('bed_type_desc', 'Bed Desc');
		
		$output          = (array) $crud->render();
		$output['title'] = 'Bed Type List';
		
		$this->load->template('room/bedtype', $output);
	}
	
	function ratetype()
	{
		$crud = new grocery_CRUD();
		
		$crud->set_table('rate_type');
		
		$crud->set_subject('Rate Type');
		$crud->where('deleted', 'No');
		
		$crud->required_fields('rate_type_code', 'rate_type_name');
		$crud->columns('rate_type_code', 'rate_type_name', 'rate_type_desc');
		
		$crud->display_as('rate_type_code', 'Rate Type Code');
		$crud->display_as('rate_type_name', 'Rate Type Name');
		$crud->display_as('rate_type_desc', 'Rate Type Description');
		
		$crud->add_fields('rate_type_code', 'rate_type_name', 'rate_type_desc');
		$crud->edit_fields('rate_type_code', 'rate_type_name', 'rate_type_desc');
		
		$crud->callback_add_field('rate_type_code', array(
			$this,
			'field_code_ratetype'
		));
		$crud->callback_delete(array(
			$this,
			'deleted_ratetype'
		));
		$crud->callback_after_update(array(
			$this,
			'get_id_ratetype'
		));
		$crud->callback_after_insert(array(
			$this,
			'get_id_ratetype'
		));
		
		$level_id = $this->session->userdata('logged_in');
		if ($level_id['level_id'] == 3) {
			$crud->unset_delete();
			$crud->unset_edit();
			
			$output          = (array) $crud->render();
			$output['title'] = 'Rate Type';
			
			$this->load->template('room/bedtype', $output);
		} else {
			$output          = (array) $crud->render();
			$output['title'] = 'Rate Type';
			
			$this->load->template('room/bedtype', $output);
			
		}
	}
	
	function deleted_ratetype($primary_key)
	{
		$level_id = $this->session->userdata('logged_in');
		$this->db->update('rate_type', array(
			'deleted' => 'Yes',
			'last_modifier' => $level_id['user_id']
		), array(
			'rate_type_id' => $primary_key
		));
		
	}
	function field_code_ratetype()
	{
		$query  = "select max(rate_type_code) as max from rate_type";
		$result = $this->db->query($query)->row();
		$string = (int) $result->max + 1;
		return '<input type="text" maxlength="4" value=" ' . $string . ' " name="rate_type_code" style="width:auto">';
		
	}
	function get_id_ratetype($post_array, $primary_key)
	{
		$sess = $this->session->userdata('logged_in');
		$this->db->update('rate_type', array(
			'last_modifier' => $sess['user_id']
		), array(
			'rate_type_id' => $primary_key
		));
		
	}
	
	function formatRupiahRoomType($value)
	{
		return 'Rp ' . number_format($value, 2, ',', '.');
	}
	
	function formatPriceFieldRoomType($value = '')
	{
		return 'Rp <input type="number" maxlength="10" value="' . $value . '" name="room_type_price" style="width:auto"> ,00';
	}
	
	function rate()
	{
		$data['css_files'] = false;
		$data['title']     = 'Room Rate';
		
		$this->load->model('room_model');
		$data['room_type'] = $this->room_model->room_type()->result_array();
		$data['rate_type'] = $this->room_model->rate_type()->result_array();
		$this->load->template('room/roomrate', $data);
	}
	
	function loadRateAjax()
	{
		$this->load->model('room_model');
		
		$data = array();
		
		foreach ($this->room_model->listRateAjax($this->input->post('rate_type_id'), $this->input->post('room_type_id'))->result_array() as $row) {
			$data[] = array(
				'id' => $row['room_rate_id'],
				'title' => '[' . $row['rate_type_name'] . '] ' . '[' . $row['room_type_name'] . '] ' . 'Rp ' . number_format($row['room_rate_price'], 2, ',', '.'),
				'start' => $row['room_rate_date'],
				'allDay' => true,
				'className' => '',
				'price' => $row['room_rate_price'],
				'rate_type_id' => $row['rate_type_id'],
				'rate_type_name' => $row['rate_type_name'],
				'room_type_id' => $row['room_type_id'],
				'room_type_name' => $row['room_type_name']
			);
		}
		
		echo json_encode($data);
	}
	
	function addRateAjax()
	{
		$this->load->model('room_model');
		
		$logged_in = $this->session->userdata('logged_in');
		
		$room_rate = array(
			'rate_type_id' => $this->input->post('rate_type'),
			'room_type_id' => $this->input->post('room_type'),
			'room_rate_date' => $this->input->post('date'),
			'room_rate_price' => $this->input->post('price'),
			'last_modifier' => $logged_in['user_id']
		);
		$this->room_model->addRateAjax($room_rate);
	}
	
	function editRateAjax()
	{
		$this->load->model('room_model');
		
		$logged_in = $this->session->userdata('logged_in');
		
		$room_rate = array(
			'rate_type_id' => $this->input->post('rate_type'),
			'room_type_id' => $this->input->post('room_type'),
			'room_rate_price' => $this->input->post('price'),
			'last_modifier' => $logged_in['user_id']
		);
		$this->room_model->editRateAjax($this->input->post('id'), $room_rate);
	}
	
	function delRateAjax()
	{
		$this->load->model('room_model');
		$this->room_model->delRateAjax($_POST['id']);
	}
	function loadRateAjax1()
	{
		$this->load->model('room_avail');
		
		foreach ($this->room_avail->listRateAjax($_POST['room_number']) as $row) {
			$data[] = array(
				'id' => $row['resv_id'],
				'title' => $row['gabungan'],
				
				'start' => $row['resv_room_date'],
				'allDay' => true,
				'className' => 'cal'
				
			);
		}
		
		echo json_encode($data);
	}
	function avail()
	{
		$data['css_files'] = false;
		$data['title']     = 'Room Available';
		
		$this->load->model('room_avail');
		$data['rate_type'] = $this->room_avail->listRateType();
		$this->load->template('room/roomavailability', $data);
	}
	
	function load_room_ajax()
	{
		$this->load->model('room_avail');
		
		$month = $this->input->post('month');
		$year  = $this->input->post('year');
		
		$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
		
		$room_type = $this->room_avail->select_room_type()->result_array();
		$rt        = array();
		foreach ($room_type as $row) {
			$rooms = $this->room_avail->select_room_by_type($row['room_type_id'])->result_array();
			$row['rooms'] = array();
			foreach ($rooms as $row2) {
				for ($i = 0; $i < $days; $i++) {
					$date           = $year . '-' . $month . '-' . ($i + 1);
					$row2['days'][] = $this->room_avail->select_resv_room($date, $row2['room_id'])->row_array();
				}
				$row['rooms'][] = $row2;
			}
			$rt['room_type'][] = $row;
		}
		$rt['days_number'] = $days;
		
		echo json_encode($rt);
		
	}	
	
	function formatPriceField($value = '') {
        return 'Rp <input class="rp" type="text" value="' . $value . '" name="room_type_price" style="width:auto">';
    }
	
	function formatAdultField($value = '') {
        return '<input class="org" type="text" value="' . $value . '" name="room_type_max_adult" style="width:auto">';
    }
	
	function formatChildField($value = '') {
        return '<input class="org" type="text" value="' . $value . '" name="room_type_max_child" style="width:auto">';
    }
	
	function formatInfantField($value = '') {
        return '<input class="org" type="text" value="' . $value . '" name="room_type_max_infant" style="width:auto">';
    }
	
	function cb_before_room_type($post) {
		$post['room_type_price'] = $this->load->comma_to_point($post['room_type_price']);
		
		return $post;
	}
	
	function ooo_room() {
		$data['css_files'] = false;
		$data['title']     = 'Choose Out of Order Room';
		
		$this->load->view('room/ooo_room', $data);
	}
	
	function make_ooo() {
		$room_id = $this->input->post('room_id');
		$date = $this->input->post('date');
		$sess = $this->session->userdata('logged_in');
		
		$resv_room = array(
			'resv_status' => 'Out of Order',
			'room_id' => $room_id,
			'resv_room_date' => $date,
			'last_modifier' => $sess['user_id']
		);
		
		$this->load->model('reservation_model');
		$id = $this->reservation_model->set_resv_room($resv_room);
		
		echo json_encode($id);
	}
	
	function make_avail() {
		$resv_room_id = $this->input->post('resv_room_id');
		$sess = $this->session->userdata('logged_in');
		
		$resv_room = array(
			'last_modifier' => $sess['user_id'],
			'deleted' => 'Yes'
		);
		
		$this->load->model('reservation_model');
		$ar = $this->reservation_model->update_resv_room($resv_room_id, $resv_room);
		
		echo json_encode($ar);
	}
	
	function move_guest($resv_room_id) {
		$data['css_files'] = false;
		$data['title']     = 'Choose Substitute Room';
		$data['resv_room_id'] = $resv_room_id;
		
		$this->load->view('room/change_room', $data);
	}
	
	function change_room() {
		$resv_room_id = $this->input->post('resv_room_id');
		$room_id = $this->input->post('room_id');
		$date = $this->input->post('date');
		$rate = $this->input->post('rate');
		$price = $this->input->post('price');
		$sess = $this->session->userdata('logged_in');
		
		$resv_room = array(
			'room_id' => $room_id,
			'resv_room_date' => $date,
			'price' => $price,
			'last_modifier' => $sess['user_id']
		);
		if($rate != 0) $resv_room['rate_type_id'] = $rate;
		else $resv_room['rate_type_id'] = null;
		
		$this->load->model('reservation_model');
		$ar = $this->reservation_model->update_resv_room($resv_room_id, $resv_room);
		
		echo json_encode($ar);
	}
	
	function get_room_price() {
		$room_id = $this->input->post('room_id');
		$date = $this->input->post('date');
		
		$this->load->model('reservation_model');
		$price[0]['type'] =  '-- Standar --';
		$price[0]['price'] = $this->reservation_model->get_std_price($room_id);
		
		$rate = $this->reservation_model->get_rate_price($room_id, $date)->result_array();
		foreach($rate as $row) {
			$p['type'] = $row['rate_type_name'];
			$p['price'] = $row['room_rate_price'];
			$price[$row['rate_type_id']]=$p;
		}
		
		echo json_encode($price);
	}
	
	function clear_unknown_resv() {
		$this->load->model('reservation_model');
		$ar = $this->reservation_model->delete_unknown_reservation();
		
		echo json_encode($ar);
	}
	
}
